package com.wys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wys.dto.SetmealDto;
import com.wys.pojo.Setmeal;
import com.wys.result.R;
import com.wys.service.SetmealService;
import com.wys.service.SetmealDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService stmealDishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *  新增套餐菜品
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> insert(@RequestBody SetmealDto setmealDto){
        R<String> stringR = setmealService.insert(setmealDto);
        /* //套餐新增后，清除redis中缓存的菜品信息(全局清理)
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);*//*
        //套餐新增后，清除redis中缓存的菜品信息(局部清理)
        String key = "dish_"+setmealDto.getCategoryId()+"status_"+setmealDto.getStatus();
        redisTemplate.delete(key);*/
        return stringR;
    }
    /**
     *  分页查询
     * @param page 当前页
     * @param pageSize 页面大小
     * @param name 查询的套餐名称
     * @return
     */
    @GetMapping("/page")
    public R<Page> getPage(int page,int pageSize,String name){
        Page pageInfo = setmealService.getPage(page, pageSize, name);
        //如果查询出来的数据的总页面小于当前页,重新查询
        if(page > pageInfo.getPages()){
            pageInfo = setmealService.getPage((int)pageInfo.getPages(),pageSize,name);
        }
        return R.success(pageInfo);
    }

    /**
     *   删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> deleteWithSetmeal(@RequestParam List<Long> ids){
        setmealService.deleteWithSetmeal(ids);
        return R.success("套餐删除成功！");
    }

    /**
     *  修改套餐状态
     * @param status  要修改的状态
     * @param ids  要修改的id数组
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateWithStatus(@PathVariable Integer status,@RequestParam List<Long> ids){

        setmealService.updateWithStatus(status,ids);
        return R.success("状态修改成功！");
    }

    /**
     *  根据ID查询套餐信息，回显示数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getWithSetmealById(@PathVariable Long id){

        return setmealService.getWithSetmealById(id);
    }

    /**
     *  修改套餐信息
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateWithSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithSetmeal(setmealDto);
       /* *//* //套餐修改后，清除redis中缓存的菜品信息(全局清理)
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);*//*
        //套餐修改后，清除redis中缓存的菜品信息(局部清理)
        String key = "dish_"+setmealDto.getCategoryId()+"status_"+setmealDto.getStatus();
        redisTemplate.delete(key);*/
        return R.success("套餐信息修改成功！");
    }

    /**
     *  移动端查询套餐
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<SetmealDto>> getSetmealMove(Setmeal setmeal){

        return setmealService.getSetmealMove(setmeal);
    }
}
